package com.czxy.tanhua.controller;

import com.czxy.tanhua.entity.UserInfo;
import com.czxy.tanhua.interceptor.UserHolder;
import com.czxy.tanhua.service.UserInfoService;
import com.czxy.tanhua.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 19:52
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    /**
     * 登录
     * @param map
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity loginUser(@RequestBody Map map){
        System.out.println(map);
        String phone = (String) map.get("phone");
        userService.loginUser(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 登录验证码和手机号注册
     * @param map
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){
        //参数为手机号和验证码
        Map result = userService.loginVerification(map);
        return ResponseEntity.ok(result);
    }

    /**
     * 完善登录信息
     * @param userInfo
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo , @RequestHeader("Authorization") String token){
//        boolean flag = JwtUtils.verifyToken(token);
//        if (!flag){
//            return ResponseEntity.status(401).body(null);
//        }
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = Long.valueOf(claims.get("id").toString());
        userInfo.setId(UserHolder.getUserId());
        userInfoService.adduserinfo(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 完善信息的头像信息
     * @param headPhoto
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head( MultipartFile headPhoto, @RequestHeader("Authorization") String token){
//        //获得用户id
//        boolean verifyToken = JwtUtils.verifyToken(token);
//
//        if (!verifyToken){
//            return ResponseEntity.status(401).body(null);
//        }
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = Long.valueOf(claims.get("id").toString());
        userInfoService.uploadHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }
}
